/*
 * Copyright 2007 - The JDPF Project Team (http://www.jdpf.org) 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */
package org.jdpf.plugins.rgbimage.serializer.file.imagedirectoryserializer;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.imageio.ImageIO;

import org.jdpf.core.kernel.elements.Token;
import org.jdpf.core.plugins.blocks.AbstractSerializerPlugin;
import org.jdpf.core.plugins.datatypes.AbstractDataType;
import org.jdpf.plugins.datatypes.media.RGBImage;

/**
 * @author Bruno Farina
 *
 */
public class ImageDirectorySerializer extends AbstractSerializerPlugin {
	
	private String _outputDirectory;
	private String _outputFileExtension;
	private String _outputFileName;

	public ImageDirectorySerializer(String transitionId, String transitionName) {
		super(transitionId, transitionName);
	}

	@Override
	public Token process(List<Token> list) {
		percentage=0.0;
		List<AbstractDataType> inList=list.get(0).getValueList();
		Iterator<AbstractDataType> iter=inList.iterator();
		List<RGBImage> imageList=new ArrayList<RGBImage>();
		int i=0;
		double tot=0.0;
		while(iter.hasNext()){
			RGBImage img=(RGBImage) iter.next();
			tot+=img.getH()*img.getW();
			imageList.add(i,img);		
			i++;
		}
		File f=new File(_outputDirectory);
		if(!f.exists()&&!f.isDirectory()){
			f.mkdirs();
		}
		for(int j=0;j<imageList.size()&&start;j++){
			RGBImage img=(RGBImage) inList.get(j);
			int[] data=(int[]) img.getValue();
			BufferedImage outImage=new BufferedImage(img.getW(),img.getH(),BufferedImage.TYPE_INT_RGB);
			outImage.setRGB(0,0,img.getW(),img.getH(),data,0,img.getW());
			String name=null;
			if(_outputFileName!=null){
				String fullFileName=_outputDirectory+System.getProperty("file.separator")+_outputFileName;
				name=fullFileName+j+"."+_outputFileExtension;
			}else{
				name=_outputDirectory+System.getProperty("file.separator")+img.getFileName()+"."+_outputFileExtension;
			}

			try {
				ImageIO.write(outImage,_outputFileExtension, new File(name));
			} catch (IOException e) {
				this.error(this, "Unable to write image in file"+name);
			}
			percentage=percentage+(data.length+1)/tot*100;
			this.log(this, "Data successfully saved in file: "+name);
		}
		return new Token();
	}

	public void setOutputDirectory(String outputDirectory) {
		this._outputDirectory = outputDirectory;
	}

	public void setOutputFileExtension(String outputFileExtension) {
		this._outputFileExtension = outputFileExtension;
	}

	public void setOutputFileName(String outputFileName) {
		this._outputFileName = outputFileName;
	}

}
